/*
 * grainanalysis_data.cpp
 *
 *  Created on: 2009. 5. 15
 *      Author: neogeo94
 */

#include "grainanalysis_data.h"

void PrintGrainAnalysisData(CEntities* pEntities){
	CEntities* Entities;
	Entities=pEntities;

	int totalNumVertex;
	totalNumVertex=0;

	CPolyline* tempPL;
	CVertex* tempVT;

	std::string fileName;
	fileName="grainanalysis.dxf";

	std::ofstream outputFile;
	outputFile.open(fileName.c_str());

	WriteDXFBegin(&outputFile);

	tempPL=Entities->Polylines;
	while (tempPL!=NULL) {
		tempVT=tempPL->Vertexs;
		WritePLineBegin(&outputFile);
		while (tempVT!=NULL) {
			//outputFile<<tempVT->x<<", "<<tempVT->y<<std::endl;
			WriteVertex(&outputFile, tempVT->x, tempVT->y);
			tempVT=tempVT->next;
		}
		//outputFile<<"999999.0, 999999.0"<<std::endl;
		WritePLineEnd(&outputFile);
		tempPL=tempPL->next;
	}

	WriteDXFEnd(&outputFile);

	outputFile.close();

}

void WriteDXFBegin(std::ofstream* out) {

	*out<<"0"<<std::endl;
	*out<<"SECTION"<<std::endl;
	*out<<"2"<<std::endl;
	*out<<"ENTITIES"<<std::endl;
}

void WriteDXFEnd(std::ofstream* out) {
	*out<<"0"<<std::endl;
	*out<<"ENDSEC"<<std::endl;
	*out<<"0"<<std::endl;
	*out<<"EOF"<<std::endl;
}

void WritePLineBegin(std::ofstream* out) {
	*out<<"0"<<std::endl;
	*out<<"POLYLINE"<<std::endl;
	*out<<"8"<<std::endl;
	*out<<"7"<<std::endl;
	*out<<"6"<<std::endl;
	*out<<"CONTINUOUS"<<std::endl;
	*out<<"66"<<std::endl;
	*out<<"1"<<std::endl;
}

void WritePLineEnd(std::ofstream* out) {
	*out<<"0"<<std::endl;
	*out<<"SEQEND"<<std::endl;
	//*out<<"8"<<std::endl;
	//*out<<"0"<<std::endl;
}

void WriteVertex(std::ofstream* out, double px, double py) {
	*out<<"0"<<std::endl;
	*out<<"VERTEX"<<std::endl;
	*out<<"8"<<std::endl;
	*out<<"7"<<std::endl;
	*out<<"10"<<std::endl;
	*out<<px<<std::endl;
	*out<<"20"<<std::endl;
	*out<<py<<std::endl;
}

